package com.btl.cygf;

import java.util.LinkedList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.btl.bean.ZuiXin;
import com.btl.utils.DownTuUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyFriendActivity extends Activity{
	private LinkedList<ZuiXin> listItems = new LinkedList<ZuiXin>(); // 数据源集合
	private MyListAdapter myListAdapter;
	private ListView listView;
	private String username;
	private AsyncHttpClient client;
	private RequestParams params;
	
	private ImageView login_backtofragment;
	
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.friend);
		Intent intent=getIntent();
		username=intent.getStringExtra("id");
		listView=(ListView) findViewById(R.id.list_friend);
		/**
		 * 异步加载网络资源信息
		 */
		client = new AsyncHttpClient();
		// 像网络传输的数据
		params = new RequestParams();
		
		getfriend(username);
		
		myListAdapter = new MyListAdapter();
		listView.setAdapter(myListAdapter);
		
		login_backtofragment = (ImageView) findViewById(R.id.login_backtofragment);
		login_backtofragment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		
	}



	private void getfriend(String username2) {
		params.add("id" , username2);
		client.post(getResources().getString(R.string.url) +"/CYGF/sendFriendJason.post", params, new JsonHttpResponseHandler() {
			// 链接成功做出的反应
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONArray response) {
				for (int i = 0; i < response.length(); i++) {
					ZuiXin zuiXin = new ZuiXin();
					JSONObject object = response.optJSONObject(i);
					String s1 = object.optString("f_id");
					String s2 = object.optString("p_nickname");
					String s3 = object.optString("p_intro");
					String s4 = object.optString("p_avatar");
					if (s2 == null) {
						zuiXin.setUsername(s1);
					} else {
						zuiXin.setUsername(s2);
					}
					zuiXin.setPid(s1);
					zuiXin.setPicture(s4);
					zuiXin.setInfo(s3);
					listItems.add(zuiXin);
					zuiXin = null;
				}
				// BaseAdapter数据发生改变
				myListAdapter.notifyDataSetChanged();
				super.onSuccess(statusCode, headers, response);
			}
			// 链接网络失败，做出的反应
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONArray errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
			}
		});
	    
	}
	/**
	 * listview的适配器MyListAdapter
	 */
	private class MyListAdapter extends BaseAdapter {

		class ListItemView {
			public ImageView image;
			public TextView name;
			public TextView intro;
		}

		@Override
		public int getCount() {
			return listItems.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
     
		@SuppressLint("SimpleDateFormat")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Intent intent = new Intent();
					intent.setClass(MyFriendActivity.this,ZuoZheActivity.class);
					intent.putExtra("id",listItems.get(position).getPid());
					startActivity(intent);
					
				}
		});
			
			// final int selectID = position;
			// 自定义视图
			ListItemView listItemView = null;
			if (convertView == null) {
				listItemView = new ListItemView();
				// 获取list_item布局文件的视图
				convertView = LayoutInflater.from(getApplicationContext()).inflate(
						R.layout.guanzhuderen_item_layout, null);
				// 获取控件对象
				listItemView.image = (ImageView) convertView
						.findViewById(R.id.touxiang);
				listItemView.name = (TextView) convertView
						.findViewById(R.id.name);
				listItemView.intro = (TextView) convertView
						.findViewById(R.id.gexinqianming);
				// 设置控件集到convertView
				convertView.setTag(listItemView);
			} else {
				listItemView = (ListItemView) convertView.getTag();
			}

			// 设置文字和图片
			ImageLoader imageLoader = ImageLoader.getInstance();

			imageLoader.init(DownTuUtils.getConfig(MyFriendActivity.this));

			imageLoader.displayImage(listItems.get(position).getPicture(), listItemView.image,DownTuUtils.getOptions2());
			// listItemView.image.setImageBitmap(new BitmapFactory().decodeByteArray(data, offset, length))
			listItemView.name.setText(listItems.get(position).getUsername());
			listItemView.intro.setText(listItems.get(position).getInfo());
			return convertView;
		}
		
	}
}
